import { Button, Form, message, Modal } from 'antd';
import { observable, action, extendObservable, isObservable, toJS } from 'mobx';
import { WeaSearchGroup, WeaTools, WeaDialog, WeaLocaleProvider } from 'ecCom';
import * as smsManageApi from '../../apis/smsManage';
import { WeaForm, WeaSwitch } from 'comsMobx';

import { WeaTableNew } from 'comsMobx';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;


export default class SmsManageStore {
	@observable comsWeaTableStore = new TableStore();

	// 用于控制传给weatable的sessionkey
    @observable dataKey = '0';

    @observable loading = false;

	// 高级搜索的form
    @observable form = new WeaForm();

    @observable viewcondition = 'none';

    @observable pageName = '';

	// 用于控制高级搜索的显隐的变量
    @observable ifShowSearchAd = false;


    // 控制新建或添加的弹窗的显隐
    @observable ifShowAddDialog = false;

    @observable viewSms = false;

    // 查看日程详细时会用到的id
    @observable smsId = '';

	// 短信管理权限
	@observable manageRight = '';

    @action
    setSmsId(id) {
        this.smsId = id;
    }

    // 这里拿到的bool是切换显隐前的显隐的bool值
    @action
    setShowSearchAd(bool) {
        this.ifShowSearchAd = bool;
    }

	@action
    getMySms(params) {
		return smsManageApi.getMySms(params);
    }

    @action
    getAllSms(params) {
		return smsManageApi.getAllSms(params);
    }

    @action
    doCancel(params) {
		return smsManageApi.doCancel(params);
    }


	@action
    doDel(params) {
		return smsManageApi.doDel(params);
    }


	@action
    reSend(params) {
		return smsManageApi.reSend(params);
    }


	// 在普通搜索完成输入点击搜素时更新表单的默认值
    @action setFormFields(value) {
        this.form.updateFields(value, true); // true代表完全覆盖方式更新条件值
    }

    @action
    setLoading(newBool = false) {
        this.loading = newBool;
    }

    @action setViewcondition(newVc) {
        this.viewcondition = newVc;
    }

    @action setPageName(pn) {
		this.pageName = pn;
    }

	initData = () => {
		// 在初始化数据开始前应该有进度条表示数据正在加载
		this.setLoading(true);
		if (this.pageName === 'mySms') {
			this.manageRight = 'y';
			smsManageApi.getMySmsCondition().then((data) => {
				const { conditioninfo } = data;
				this.conditioninfo = conditioninfo;
				this.form = new WeaForm();
				// 根据高级搜索条件初始化form
				this.form.initFormFields(conditioninfo);
				this.setFormFields({ timeSag__fromdate__enddate: { value: '1' } });
				this.doSearch();
			});
		} else if (this.pageName === 'allSms') {
			smsManageApi.getAllSmsCondition().then((data) => {
				const { conditioninfo, canManage } = data;
				if (canManage === '1') {
					this.manageRight = 'y';
					this.conditioninfo = conditioninfo;
					this.form = new WeaForm();
					// 根据高级搜索条件初始化form
					this.form.initFormFields(conditioninfo);
					this.setFormFields({ timeSag__fromdate__enddate: { value: '1' } });
					this.doSearch();
				} else {
					this.manageRight = 'n';
				}
			});
		}
		this.setViewcondition(1);
	};

    @action doSearch = () => {
        // 在搜索开始前应该有进度条表示数据正在加载
        this.setLoading(true);
		let searchParamsAd = this.form.getFormParams();
        const searchParamsData = this.form.getFormDatas();
        if (this.pageName === 'mySms') {
			smsManageApi.getMySms(searchParamsAd).then((data) => {
				this.comsWeaTableStore.getDatas(data.sessionkey, 1);
				this.dataKey = data.sessionkey;
				this.setLoading(false); // 加载完数据清除进度条
			});
        } else if (this.pageName === 'allSms') {
			smsManageApi.getAllSms(searchParamsAd).then((data) => {
				this.comsWeaTableStore.getDatas(data.sessionkey, 1);
				this.dataKey = data.sessionkey;
				this.setLoading(false); // 加载完数据清除进度条
			});
        }
		if (searchParamsAd.timeSag) {
			this.setViewcondition(searchParamsAd.timeSag);
		}
    }

    // 获取form当前的值
    @action
    getFormParams() {
        return this.form.getFormParams();
    }

    @action
    onEnterSearch = () => {
      this.doSearch();
      this.setShowSearchAd(false);
    }

    // 用于获取高级搜索中的表单控件
	@action getSearchs(store) {
		const FormItem = Form.Item;
		// const conditioninfo = adFormdatas.condition;
		let { form, conditioninfo } = store;
		conditioninfo = toJS(conditioninfo);
		const { isFormInit } = form;
		let group = [];
		// 获取表单的值
		const formParams = form.getFormParams();
		// isFormInit 为true，form已经初始化完成
		isFormInit && conditioninfo.map((c) => {
			let items = [];
			c.items.map((fields) => {
				items.push({
					com: (<FormItem ecId={`${this && this.props && this.props.ecId || ''}_FormItem@dz4245@${(fields.domkey && fields.domkey[0]) ? fields.domkey[0] : 'dz4245'}`}
  label={`${fields.label}`}
  labelCol={{ span: `${fields.labelcol}` }}
  wrapperCol={{ span: `${fields.fieldcol}` }}
					>
						{/* 根据 WeaSwitch 获取对应的组件 */}
						<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@ejv896@${(fields.domkey && fields.domkey[0]) ? fields.domkey[0] : 'ejv896'}`}
  fieldConfig={fields}
  form={form}
  formParams={formParams}
  onEnterSearch={this.onEnterSearch}
						/>
					</FormItem>),
					colSpan: 1,
				});
			});
			group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@zoonut@WeaSearchGroup@zoonut`} needTigger={true} showGroup={c.defaultshow} items={items} />);
		});
		return group;
	}

	// 定义tab中点击高级搜索时的最下面的操作按钮集合
	@action getTabButtonsAd(store) {
		return [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@khq0k4@searchBtn`}
  type="primary"
  onClick={() => {
					// 使得高级搜索在点击搜索按钮2后也能跳转到相应的tab页
					store.doSearch();

					store.setShowSearchAd(false);
				}}
			>
				{getLabel(197, '搜索')}
			</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ebft5z@resetBtn`} type="ghost" onClick={() => { store.resetAdSearchForm(); }}>{getLabel(2022, '重置')}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@iiv93v@closeBtn`} type="ghost" onClick={() => { store.setShowSearchAd(false); }}>{getLabel(31129, '取消')}</Button>),
		];
	}

	// 打开生成日程弹窗
    @action
    openViewSms() {
        this.viewSms = true;
    }

    @action
    closeViewSms() {
        this.viewSms = false;
	}

	// 清空高级搜索表单里面所有字段的值
    @action resetAdSearchForm() {
		if (this.pageName === 'mySms') {
			this.setFormFields({ msg: { value: '' }, timeSag__fromdate__enddate: { value: '1' }, messagestatus: { value: '' }, messagetype: { value: '' } });
		} else {
			this.setFormFields({ msg: { value: '' }, timeSag__fromdate__enddate: { value: '1' }, messagestatus: { value: '' }, messagetype: { value: '' }, objType: { value: '1' }, deleted: { value: '' } });
		}
    }
}
